package fun.flyee.android.pengshan.acts;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import androidx.fragment.app.FragmentTransaction;
import fun.flyee.pengshan.social.R;
import fun.flyee.android.pengshan.fgts.BaseFragment;
import fun.flyee.android.pengshan.fgts.LinkFragment;
import fun.flyee.android.pengshan.fgts.WebFragment;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.widgets.Tips;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FuncActivity extends BaseActivity {

    protected BaseFragment mFragment;

    private String url;
    private String path;
    private String query;
    private String payload;

    @Override
    protected int layoutId() {
        return R.layout.act_container;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            path = getIntent().getStringExtra("path");
            query = getIntent().getStringExtra("query");
            if ((query != null && query.contains("screenOn")) || (path != null && path.contains("screenOn"))) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
            payload = getIntent().getStringExtra("payload");
            url = getIntent().getStringExtra("url");
            if (!TextUtils.isEmpty(path)) {
                if (TextUtils.isEmpty(payload)) {
                    mFragment = WebFragment.newInstance(path, query);
                } else {
                    mFragment = WebFragment.newInstance2(path, payload);
                }
            } else {
                mFragment = LinkFragment.newInstance(url);
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.frame_container, mFragment, "fragment_web").show(mFragment);
            transaction.commitAllowingStateLoss();
        } else {
            mFragment = (WebFragment) getSupportFragmentManager().findFragmentByTag("fragment_web");
            path = savedInstanceState.getString("path");
            query = savedInstanceState.getString("query");
            payload = savedInstanceState.getString("payload");
            url = savedInstanceState.getString("url");
            if (TextUtils.isEmpty(path)) {
                mFragment.setPath(url);
            } else {
                mFragment.setPath(path);
                WebFragment wf = (WebFragment) mFragment;
                if (TextUtils.isEmpty(payload)) {
                    wf.setQuery(query);
                } else {
                    wf.setPayload(payload);
                }
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(mFragment);
            transaction.commitAllowingStateLoss();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("path", path);
        outState.putString("url", url);
        outState.putString("query", query);
        outState.putString("payload", payload);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (mFragment != null) {
            mFragment.onActivityResult(requestCode, resultCode, intent);
        }
    }

    @Override
    public void onBackPressed() {
        if (mFragment != null) {
            if (mFragment instanceof WebFragment) {
                ((WebFragment) mFragment).onBackPressed();
            } else {
                ((LinkFragment) mFragment).onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
